# coding: utf-8

from service import UserService
import pylog
import tornado.ioloop
import tornado.web

logger = pylog.init("./log/")

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Welcome!")
        
class LoginHandler(tornado.web.RequestHandler):
    def get(self):
        username = self.get_argument("username", "")
        password = self.get_argument("password", "")
        appkey = self.get_argument("appkey", "")
        with UserService() as us:
            data = us.login(username, password, appkey)
        self.write("{ret: %s, type: %s}" % (data[0], data[1]))
        
class RegisterHandler(tornado.web.RequestHandler):
    def get(self):
        with UserService() as us:
            data = us.register()
        self.write(data)

application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/user/login", LoginHandler),
    (r"/user/register", RegisterHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
